package com.ylxl.controller;

import com.ylxl.common.constant.Constants;
import com.ylxl.common.domain.SysUser;
import com.ylxl.common.result.AjaxResult;
import com.ylxl.common.utils.SecurityUtils;
import com.ylxl.request.LoginBody;
import com.ylxl.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录管理
 *
 */
@Slf4j
@RestController
@RequestMapping()
@Api(tags = "登录管理", hidden = true)
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录接口", response = AjaxResult.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = String.class),
    })
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    @ApiOperation(value = "获取用户信息", response = AjaxResult.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = SysUser.class),
    })
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }
}
